﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using PostSharp.Aspects;
using PostSharp.Extensibility;

namespace BusinessFramework.AOP
{
    [MulticastAttributeUsage(MulticastTargets.Class, Inheritance = MulticastInheritance.Strict)]
    [Serializable]
    public sealed class NotifyPropertyChangingAttribute : TypeLevelAspect, IAspectProvider
    {
        public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
        {
            var type = targetElement as Type;
            if (type == null)
                return new AspectInstance[0];

            if (typeof(INotifyPropertyChanging).IsAssignableFrom(type))
                return new[] { new AspectInstance(targetElement, new AddNotifyPropertyChangingAttribute()) };
            else
                return new[] { new AspectInstance(targetElement, new ImplementNotifyPropertyChangingAttribute()) };
        }
    }
}